import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service";
import { NotificationService } from "../../shared/utils/notification.service";
import { config } from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { throttleable } from '@swimlane/ngx-datatable/release/utils';


declare var $: any;
@Component({
	selector: 'sa-fault-problem',
	templateUrl: './fault-problem.component.html',
	styleUrls: ['./fault-problem.css']
})
export class FaultProblemComponent implements OnInit {
	validatorOptions = {
		excluded:[":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			startTime:{
				trigger:"change",
				validators: {
					notEmpty: {
						message: '请选择开始时间'
					},
					date: {
                        format: 'YYYY-MM-DD',
                        message: '请输入正确的时间,例如2018-01-00'
                    }
				}
			},
			endTime:{
				trigger:"change",
				validators: {
					notEmpty: {
						message: '请选择完成时间'
					},
					date: {
                        format: 'YYYY-MM-DD',
                        message: '请输入正确的时间,例如2018-01-00'
                    }
				}
			}
		}
	};
	constructor(private jsonApiService: JsonApiService, private notificationService: NotificationService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {});
	}
	tableData:any[] = []; //table数据
	totalCounts:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	startTime:string = '';//过滤条件:开始时间
	endTime:string = '';//过滤条件:结束时间
	searchKey:string = ''; //过滤条件:keyword
	selectedCompanyName = '';//过滤条件:公司名称
	selectedCompanyId = '';//过滤条件:公司ID
	btnNum:string = 'repairCount';
	startTime_table:string = '';
	//table   分页
	load(){
		this.startTime_table = $('#startTimePicker').val();
	    if(!this.startTime_table){
            return this.jsonApiService.message('时间不能为空');
        }
		
		const param = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				startTime:this.startTime_table + ' 00:00:00',
				endTime:this.startTime_table + ' 23:59:59',
				companyId:this.selectedCompanyId,
				// searchKey:this.searchKey.trim(),
				sort: this.columnSortFlag
			}
		};
		let reqdata = 'gridPager=' + JSON.stringify(param);
		this.jsonApiService.http("environmentManager/breakdown/problemStatisticsList.html", reqdata).subscribe(data => {
			if(data&&data.exhibitDatas){
				this.tableData = data.exhibitDatas;
				this.totalCounts = data.recordCount;
			}
		})
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker1").datetimepicker("setStartDate", null);
		$('.TimeSuggest').val('')
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//查询
	table_search(){
		this.curPage = 1;
		this.load();
	}
	//数据分类按钮点击事件
	btnClick(type){
		this.btnNum = type;
		this.curPage = 1;
		this.columnSortFlag = '';
		this.load();
	}
	//刷新
	refresh(){
		this.load();
	}

	/*
	 *  数据导出
	 */
	exportReport(){
		if(!this.startTime_table){
			return this.jsonApiService.message('时间不能为空');
		}
		const param = {
			startTime:this.startTime_table + ' 00:00:00',
			endTime:this.startTime_table + ' 23:59:59',
			companyId:this.selectedCompanyId,
			companyName:this.selectedCompanyName,
			searchKey:common.stringTrim(this.searchKey),
			type:'',
			sort: this.columnSortFlag
		};
		switch (this.btnNum){
			case 'sensorProblem':
				param.type = '2';		//传感器问题统计
				break;
			case 'gpsProblem':
				param.type = '3';		//GPS问题统计
				break;
			case 'repairCount':
				param.type = '1';	//待维修报表统计	
				break;
			default:
				break;
		}	
		common.exprotDataFile(param, 'param', config.BaseURL + 'environmentManager/breakdown/problemStatisticsListExport.html')
	}
	
	//公司选中回调
	companySelected(val) {
		this.selectedCompanyId = val.companyId;
		this.selectedCompanyName = val.companyName;
	}
	ngOnInit() {
		let date = new Date();
        date.setDate(date.getDate() - 1);
        let yersterday = common.getTimeDay(date);
        this.startTime_table = yersterday;
		System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
            ()=>{System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
		        common.initDatePicker('#startTimePicker',1);
		        this.load();
		    })
        })
		// this.initDate().then(() => {
		// 	this.load();
		// });
	}

	Jump(row){
		this.router.navigate(['/environment-manage/fault-problem-detail'],{ 
			queryParams: {
				companyId:row.companyId,
				companyName: row.companyName
			}
		});
	}


	//排序
    columnSortFlag: any=''
    columnSort(name){
		this.columnSortFlag = name;
		this.load();
    }
}	
